package cn.itcast.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    private UserService userService;

    /*
     * 根据关键字查询
     * */
    @GetMapping("page")
    public Result selectByNameUser(Integer currentPage, Integer pageSize, Long total,String queryString){
        try {
            PageResult pageResult = this.userService.selectByNameUser(currentPage, pageSize, total,queryString);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    /*
    *根据id查询数据
    * */
    @GetMapping(path = "/{id}")
    public Result selectByIdUser(@PathVariable("id") Integer id){
        try {
            User user = this.userService.selectByIdUser(id);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

     /*
      * 查询用户下的所有角色id
      */
    @GetMapping(path = "/{id}/roleIds")
    public Result selectRoleIds(@PathVariable("id") Integer userId){
        try {
            Integer[] roleIds = this.userService.selectRoleIds(userId);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    /*
    * 新增用户
    * */
    @PostMapping
    public Result saveUser(@RequestBody User user,Integer[] roleIds){
        try {
            this.userService.saveUser(user,roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL);
        }
    }

    /*
     * 删除用户
     * */
    @DeleteMapping(path = "/{id}")
    public Result deleteUser(@PathVariable("id")Integer userId){
        try {
            this.userService.deleteUser(userId);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
    }

    /*
     * 修改用户
     * */
    @PutMapping
    public Result updateUser(@RequestBody User user, Integer[] roleIds){
        try {
            this.userService.updateUser(user,roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
    }

}
